home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / OutOfPhase1.01Source / OutOfPhase Folder / LinearTransition.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-01  |  5.5 KB  |  188 lines  |  [TEXT/KAHL]

  1. /* LinearTransition.c */
  2. /*****************************************************************************/
  3. /*                                                                           */
  4. /*    Out Of Phase:  Digital Music Synthesis on General Purpose Computers    */
  5. /*    Copyright (C) 1994  Thomas R. Lawrence                                 */
  6. /*                                                                           */
  7. /*    This program is free software; you can redistribute it and/or modify   */
  8. /*    it under the terms of the GNU General Public License as published by   */
  9. /*    the Free Software Foundation; either version 2 of the License, or      */
  10. /*    (at your option) any later version.                                    */
  11. /*                                                                           */
  12. /*    This program is distributed in the hope that it will be useful,        */
  13. /*    but WITHOUT ANY WARRANTY; without even the implied warranty of         */
  14. /*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          */
  15. /*    GNU General Public License for more details.                           */
  16. /*                                                                           */
  17. /*    You should have received a copy of the GNU General Public License      */
  18. /*    along with this program; if not, write to the Free Software            */
  19. /*    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.              */
  20. /*                                                                           */
  21. /*    Thomas R. Lawrence can be reached at tomlaw@world.std.com.             */
  22. /*                                                                           */
  23. /*****************************************************************************/
  24.  
  25. #include "MiscInfo.h"
  26. #include "Audit.h"
  27. #include "Debug.h"
  28. #include "Definitions.h"
  29.  
  30. #include "LinearTransition.h"
  31. #include "Memory.h"
  32.  
  33.  
  34. struct LinearTransRec
  35.     {
  36.         long                        LastValue;
  37.         long                        Remainder;
  38.         long                        Limit;
  39.         long                        ValueIncrement;
  40.         long                        RemainderIncrement;
  41.         MyBoolean                Negative;
  42.         LinearTransRec*    Link;
  43.     };
  44.  
  45.  
  46. static LinearTransRec*            DeadRecordList = NIL;
  47.  
  48.  
  49. /* open a new linear transition state record */
  50. LinearTransRec*            NewLinearTransition(long Start, long Destination, long TicksToReach)
  51.     {
  52.         LinearTransRec*        TransRec;
  53.  
  54.         if (DeadRecordList == NIL)
  55.             {
  56.                 TransRec = (LinearTransRec*)AllocPtrCanFail(sizeof(LinearTransRec),
  57.                     "LinearTransRec");
  58.                 if (TransRec == NIL)
  59.                     {
  60.                         return NIL;
  61.                     }
  62.             }
  63.          else
  64.             {
  65.                 TransRec = DeadRecordList;
  66.                 DeadRecordList = DeadRecordList->Link;
  67.             }
  68.         RefillLinearTransition(TransRec,Start,Destination,TicksToReach);
  69.         return TransRec;
  70.     }
  71.  
  72.  
  73. #if DEBUG
  74. static void                    CheckTransRec(LinearTransRec* TransRec)
  75.     {
  76.         LinearTransRec*        Scan;
  77.  
  78.         Scan = DeadRecordList;
  79.         while (Scan != NIL)
  80.             {
  81.                 if (Scan == TransRec)
  82.                     {
  83.                         PRERR(ForceAbort,"CheckTransRec:  record is on the garbage list");
  84.                     }
  85.                 Scan = Scan->Link;
  86.             }
  87.     }
  88. #else
  89. #define CheckTransRec(x) ((void)0)
  90. #endif
  91.  
  92.  
  93. /* refill a linear transition with new state information */
  94. void                                RefillLinearTransition(LinearTransRec* TransRec, long Start,
  95.                                             long Destination, long TicksToReach)
  96.     {
  97.         CheckPtrExistence(TransRec);
  98.         CheckTransRec(TransRec);
  99.         TransRec->LastValue = Start;
  100.         TransRec->Remainder = 0;
  101.         TransRec->Limit = TicksToReach;
  102.         TransRec->ValueIncrement = (Destination - Start) / TicksToReach;
  103.         TransRec->RemainderIncrement = (Destination - Start) % TicksToReach;
  104.         if (TransRec->RemainderIncrement < 0)
  105.             {
  106.                 TransRec->RemainderIncrement = - TransRec->RemainderIncrement;
  107.                 TransRec->Negative = True;
  108.             }
  109.          else
  110.             {
  111.                 TransRec->Negative = False;
  112.             }
  113.     }
  114.  
  115.  
  116. /* get rid of a linear transition state record */
  117. void                                DisposeLinearTransition(LinearTransRec* TransRec)
  118.     {
  119.         CheckPtrExistence(TransRec);
  120.         CheckTransRec(TransRec);
  121.         TransRec->Link = DeadRecordList;
  122.         DeadRecordList = TransRec;
  123.     }
  124.  
  125.  
  126. /* get rid of all cached transition state records */
  127. void                                FlushLinearTransitionRecords(void)
  128.     {
  129.         LinearTransRec*        Scan;
  130.  
  131.         Scan = DeadRecordList;
  132.         while (Scan != NIL)
  133.             {
  134.                 LinearTransRec*        Temp;
  135.  
  136.                 Temp = Scan;
  137.                 Scan = Scan->Link;
  138.                 ReleasePtr((char*)Temp);
  139.             }
  140.         DeadRecordList = NIL;
  141.     }
  142.  
  143.  
  144. /* execute one cycle and return the value */
  145. long                                LinearTransitionUpdate(LinearTransRec* TransRec)
  146.     {
  147.         long                            Adjustment;
  148.  
  149.         CheckPtrExistence(TransRec);
  150.         CheckTransRec(TransRec);
  151.         TransRec->LastValue += TransRec->ValueIncrement;
  152.         TransRec->Remainder += TransRec->RemainderIncrement;
  153.         if (TransRec->Remainder >= TransRec->Limit)
  154.             {
  155.                 Adjustment = TransRec->Remainder / TransRec->Limit;
  156.                 TransRec->Remainder -= Adjustment * TransRec->Limit;
  157.                 if (TransRec->Negative)
  158.                     {
  159.                         Adjustment = - Adjustment;
  160.                     }
  161.                 TransRec->LastValue += Adjustment;
  162.             }
  163.         return TransRec->LastValue;
  164.     }
  165.  
  166.  
  167. /* execute multiple cycles and return the value */
  168. long                                LinearTransitionUpdateMultiple(LinearTransRec* TransRec,
  169.                                             long NumCycles)
  170.     {
  171.         double                        NewRemainder;
  172.         long                            WholePortion;
  173.  
  174.         CheckPtrExistence(TransRec);
  175.         CheckTransRec(TransRec);
  176.  
  177.         TransRec->LastValue += TransRec->ValueIncrement * NumCycles;
  178.         NewRemainder = TransRec->Remainder + ((double)TransRec->RemainderIncrement * NumCycles);
  179.         WholePortion = NewRemainder / TransRec->Limit;
  180.         TransRec->Remainder = NewRemainder - ((double)TransRec->Limit * WholePortion);
  181.         if (TransRec->Negative)
  182.             {
  183.                 WholePortion = - WholePortion;
  184.             }
  185.         TransRec->LastValue += WholePortion;
  186.         return TransRec->LastValue;
  187.     }
  188.